import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Hello world!
 */
public class GroupProducer {
 
    public static void main(String[] args) throws Exception {
        thread(new HelloWorldProducer(), false);
    }
 
    public static void thread(Runnable runnable, boolean daemon) {
        Thread brokerThread = new Thread(runnable);
        brokerThread.setDaemon(daemon);
        brokerThread.start();
    }
 
    public static class HelloWorldProducer implements Runnable {
        public void run() {
            try {
                // Create a ConnectionFactory
                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.1.17:61616");
//
                // Create a Connection
                Connection connection = connectionFactory.createConnection();
                connection.start();

                // Create a Session
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                // Create the destination (Topic or Queue)
                Destination destination = session.createQueue("TEST");

                // Create a MessageProducer from the Session to the Topic or Queue
                MessageProducer producer = session.createProducer(destination);
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);

                // Create a messages producer

                for (int i=0;i< 1000;i++) {
                    TextMessage message = session.createTextMessage("hi IBM_NASDAQ" + i + ":"+"A");
                    message.setStringProperty("JMSXGroupID", "IBM_NASDAQ_20/4/05");
                    producer.send(message);
                    System.out.println("hi" + i + ":"+"A");

                    TextMessage message2 = session.createTextMessage("hi Oracle_NASDAQ" + i + ":"+"B");
                    message2.setStringProperty("JMSXGroupID", "Oracle");
                    producer.send(message2);
                    System.out.println("hi" + i + ":"+"B");

                    TextMessage message3 = session.createTextMessage("hi Google_NASDAQ" + i + ":"+"C");
                    message3.setStringProperty("JMSXGroupID", "Google");
                    producer.send(message3);
                    System.out.println("hi " + i + ":"+"C");
                }

                // Clean up
                session.close();
                connection.close();
            }
            catch (Exception e) {
                System.out.println("Caught: " + e);
                e.printStackTrace();
            }
        }
    }
 

}